package org.client.iot.ui.device.adapter;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.client.iot.R;
import org.client.iot.domain.DeviceDto;
import org.client.iot.domain.ProjectDto;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.hutool.core.collection.CollUtil;

public class DevListAdapter extends BaseQuickAdapter<DeviceDto, BaseViewHolder> implements LoadMoreModule {
    private Context mContext;

    public DevListAdapter(Context mContext, @Nullable List<DeviceDto> data) {
        super(R.layout.content_adapter_dev_item, data);
        this.mContext = mContext;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, DeviceDto deviceDto) {
        baseViewHolder.setText(R.id.textView_dev_title, deviceDto.getDeviceName());
        baseViewHolder.setText(R.id.textView_dev_type, "类型：" + deviceDto.getProductName());
    }

    @Override
    public void addData(@NonNull Collection<? extends DeviceDto> newData) {
        List<DeviceDto> data = getData();
        Set<DeviceDto> set1 = new HashSet<>(data);
        List<DeviceDto> uniqueList = Stream.concat(data.stream(), newData.stream())
                .filter(pojo -> !(set1.contains(pojo)))
                .distinct()
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(uniqueList)) {
            super.addData(newData);
        } else {
            super.addData(uniqueList);
        }
    }

    public String getString(int strId) {
        return mContext.getString(strId);
    }
}
